package cn.zh.npro.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.zh.npro.R;
import cn.zh.npro.helper.ItemDragHelperCallback;

/**
 * Created by WYN on 2018/2/26 0026.
 */

public class Homefragment extends Fragment implements View.OnClickListener {

    Fragment hmonef,hnf,hgf;
    RadioGroup hfgroup;
    FragmentManager fm;
    RadioButton odmoney,odnum,gdrank;
    ImageView addimg;
    boolean ist=true;
    ArrayList<RadioButton>rBs=new ArrayList<>();
    TextView txtvi;
    FrameLayout covfram;
    private RecyclerView mRecy;
    String[] strs={"订单金额","订单数量","商品排名"};
    List<ChannelEntity> items = new ArrayList<>();
    List<ChannelEntity> otherItems = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.hf_layout,container,false);
        fm=getActivity().getSupportFragmentManager();
        inithf(view);
        return view;
        }

    public void inithf(View view){
        hfgroup=view.findViewById(R.id.hfgroup);
        odmoney=view.findViewById(R.id.odmoney);
        rBs.add(odmoney);
        odnum=view.findViewById(R.id.odnum);
        rBs.add(odnum);
        gdrank=view.findViewById(R.id.gdrank);
        rBs.add(gdrank);
        addimg=view.findViewById(R.id.addimg);
        covfram=view.findViewById(R.id.covfram);
        txtvi = view.findViewById(R.id.empty);
        addimg.setBackgroundResource(R.mipmap.add_white);
        addimg.setOnClickListener(this);
        hfgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                FragmentTransaction ft=fm.beginTransaction();
                hideAllFragments(ft);
                setColor();
                switch (i){
                    case R.id.odmoney:
                        odmoney.setTextColor(getResources().getColor(R.color.white));
                        if (null==hmonef){
                            hmonef = new HMoney();
                            ft.add(R.id.hfram,hmonef);
                            //如果当前fragment已经被构造，则显示出来
                        }else {
                            ft.show(hmonef);
                        }
                        break;
                    case R.id.odnum:
                        odnum.setTextColor(getResources().getColor(R.color.white));
                        if (null==hnf){
                            hnf = new HnFragment();
                            ft.add(R.id.hfram,hnf);
                            //如果当前fragment已经被构造，则显示出来
                        }else {
                            ft.show(hnf);
                        }
                        break;
                    case R.id.gdrank:
                        gdrank.setTextColor(getResources().getColor(R.color.white));
                        if (null==hgf){
                            hgf = new HgFragment();
                            ft.add(R.id.hfram,hgf);
                            //如果当前fragment已经被构造，则显示出来
                        }else {
                            ft.show(hgf);
                        }
                        break;
                   }
                ft.commit();
                  }
        });
        RadioButton odmoney= (RadioButton)view.findViewById(R.id.odmoney);
        odmoney.setChecked(true);

        getview(view);
         }


    @Override
    public void onClick(View view) {
      int id=view.getId();
      switch (id){
          case R.id.addimg:
              txtvi.setVisibility(View.GONE);
              if (ist){
                  hfgroup.setVisibility(View.INVISIBLE);
                  ist=false;
                  Animation animation = AnimationUtils.loadAnimation(getActivity(),R.anim.rot);
                  addimg.setAnimation(animation);
                  addimg.startAnimation(animation);
                  covfram.setVisibility(View.VISIBLE);
                  Animation animation1 = AnimationUtils.loadAnimation(getActivity(),R.anim.showin);
                  covfram.setAnimation(animation1);
                  covfram.startAnimation(animation1);
                }else {
                  Animation animation = AnimationUtils.loadAnimation(getActivity(),R.anim.rotre);
                  addimg.setAnimation(animation);
                  addimg.startAnimation(animation);
                  hfgroup.setVisibility(View.VISIBLE);
                  ist=true;
                  covfram.setVisibility(View.GONE);
                  Animation animation1 = AnimationUtils.loadAnimation(getActivity(),R.anim.showhide);
                  covfram.setAnimation(animation1);
                  covfram.startAnimation(animation1);
                  hfgroup.removeAllViews();

                  if (items.size()==0){
                      txtvi.setVisibility(View.VISIBLE);
                      return;
                  }else {
                      txtvi.setVisibility(View.GONE);
                    }
                   for (int i=0;i<items.size();i++){
                       for (int g=0;g<rBs.size();g++){
                          if (rBs.get(g).getText().toString().equals(items.get(i).getName())){
                              hfgroup.addView(rBs.get(g));
                              rBs.get(g).setVisibility(View.VISIBLE);
                            };
                          }
                       }

                   for (int j=0;j<rBs.size();j++){
                       boolean is=true;
                       for (int k=0;k<items.size();k++){
                           if (rBs.get(j).getText().toString().equals(items.get(k).getName())){
                               rBs.get(j).setVisibility(View.VISIBLE);
                               is=false;
                            };
                          }
                       if (is){
                           rBs.get(j).setVisibility(View.GONE);
                           if (rBs.get(j).isChecked()){
                               for (int t=0;t<rBs.size();t++){
                                   if (rBs.get(t).getVisibility()==View.VISIBLE){
                                       rBs.get(t).setChecked(true);
                                       return;
                                    }
                                }
                             }
                         }
                     }
                 }
              break;
             }
          }



    @Override
    public void onSaveInstanceState(Bundle outState) {
      }

    private void hideAllFragments(FragmentTransaction ft){
        if (null != hmonef){ft.hide(hmonef);}
        if (null != hnf){ft.hide(hnf);}
        if (null != hgf){ft.hide(hgf);}
        }


      public void setColor(){
          odmoney.setTextColor(getResources().getColor(R.color.txtbg));
          odnum.setTextColor(getResources().getColor(R.color.txtbg));
          gdrank.setTextColor(getResources().getColor(R.color.txtbg));
           }


     public void getview(View view){
         mRecy = (RecyclerView)view.findViewById(R.id.recy);
         for (int i = 0; i <strs.length; i++) {
             ChannelEntity entity = new ChannelEntity();
             entity.setName(strs[i]);
             items.add(entity);
           }
         GridLayoutManager manager = new GridLayoutManager(getActivity(), 4);
         mRecy.setLayoutManager(manager);

         ItemDragHelperCallback callback = new ItemDragHelperCallback();
         final ItemTouchHelper helper = new ItemTouchHelper(callback);
         helper.attachToRecyclerView(mRecy);

         final ChannelAdapter adapter = new ChannelAdapter(getActivity(), helper, items, otherItems);
         manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
             @Override
             public int getSpanSize(int position) {
                 int viewType = adapter.getItemViewType(position);
                 return viewType == ChannelAdapter.TYPE_MY || viewType == ChannelAdapter.TYPE_OTHER ? 1 : 4;
               }
           });

         mRecy.setAdapter(adapter);
         adapter.setOnMyChannelItemClickListener(new ChannelAdapter.OnMyChannelItemClickListener() {
             @Override
             public void onItemClick(View v, int position) {
                 Toast.makeText(getActivity(), items.get(position).getName(), Toast.LENGTH_SHORT).show();
              }
           });
          }

    }
